package handledb;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;

public class HandleListener implements ServletRequestListener {

    public void requestDestroyed(ServletRequestEvent sre) {
        System.out.println("-----------------close-----------------"+sre);
        Long id = Thread.currentThread().getId();
        TransactionManage.commitAndCloseAll(null, id);
    }

    public void requestInitialized(ServletRequestEvent sre) {
        HttpServletRequest request = (HttpServletRequest)sre.getServletRequest();
        String transactionId = request.getHeader("handle_db_transaction_id");
        System.out.println("transactionId: "+transactionId);
        System.out.println(request.getServerPort());
        if(transactionId!=null && !transactionId.equals("")){
            long id = Long.valueOf(transactionId);
            if(id!=0){
                String effectiveTime = request.getHeader("effective_time");
                Long time = null;
                if(effectiveTime!=null && !effectiveTime.equals("")){
                    time = Long.valueOf(effectiveTime);
                }
                TransactionManage.putTransactionItem(time,id,null).isLocal = false;
            }
        }
    }
}
